home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / src.zoo / src / utmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-17  |  3.4 KB  |  164 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: utmp.c,v 1.1 89/03/17 08:21:23 sau Exp $
  9.     $Source: /m1/mgr.new/src/RCS/utmp.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /m1/mgr.new/src/RCS/utmp.c,v $$Revision: 1.1 $";
  12.  
  13. /* manage utmp file */
  14.  
  15. #include <pwd.h>
  16. #include <utmp.h>
  17. #include <sys/file.h>
  18. #include <sys/time.h>
  19. #include <stdio.h>
  20.  
  21. #define UTMP    "/etc/utmp"
  22. #define TTYS    "/etc/ttys"
  23.  
  24. static struct utmp    entry, save_entry;
  25. static char        zap[sizeof(entry)];
  26. static int        save_slot;
  27.  
  28. /* remove an entry from utmp file */
  29.  
  30. int
  31. rm_utmp(line)    
  32. char *line;
  33.    {
  34.    return(_rm_utmp(line,0));
  35.    }
  36.  
  37. /* remove and save an entry in the utmp file */
  38.  
  39. int
  40. save_utmp(line)
  41. char *line;
  42.    {
  43.    return(_rm_utmp(line,1));
  44.    }
  45.  
  46. /* add an entry to the utmp file */
  47.  
  48. int
  49. add_utmp(fd,host)
  50. int fd;
  51. char *host;
  52.    {
  53.    return(_add_utmp(fd,host,0));
  54.    }
  55.  
  56. /* restore a previously saved utmp file entry */
  57.  
  58. int
  59. restore_utmp(fd,host)
  60. int fd;
  61. char *host;
  62.    {
  63.    return(_add_utmp(fd,host,1));
  64.    }
  65.  
  66. /* defined here so we needn't include defs.h */
  67.  
  68. #ifdef SYSV
  69. #define index        strchr
  70. #define rindex        strrchr
  71. #endif
  72.  
  73. /* utmp add-entry service routine */
  74.  
  75. int
  76. _add_utmp(fd,host,flag)
  77. int fd;
  78. char *host;
  79. int flag;
  80.    {
  81.    char *ttyname(), *rindex();
  82.    long time();
  83.    struct passwd *getpwuid();
  84.    struct timeval tp;
  85.    char *line = ttyname(0);
  86.    int tty;
  87.    int tell;
  88.  
  89.    if ((fd = open(UTMP,O_RDWR)) >= 0) {
  90.       if (flag) {
  91.          lseek(fd,(long) (save_slot*sizeof(entry)),0);
  92.          write(fd,&save_entry,sizeof(entry));
  93.          }
  94.       else if (line && (tty=ttyslot())) {
  95.  
  96.          lseek(fd,(long) (tty*sizeof(entry)),0);
  97.          if (rindex(line,'/'))
  98.             line = rindex(line,'/')+1;
  99.  
  100.          strncpy(entry.ut_line, line, sizeof entry.ut_line);
  101.          strncpy(entry.ut_name, getpwuid(getuid())->pw_name,
  102.         sizeof entry.ut_name);
  103.          if (host == (char *) 0)
  104.             strcpy(entry.ut_host, "");
  105.          else
  106.             strncpy(entry.ut_host, host, sizeof entry.ut_host);
  107.          gettimeofday(&tp,0);
  108.          entry.ut_time = tp.tv_sec;
  109.       
  110.          write(fd,&entry,sizeof(entry));
  111.          }
  112.       close(fd);
  113.       return(tty);
  114.       }
  115.    return(-1);
  116.    }
  117.  
  118. /* remove utmp entry service routine */
  119.  
  120. int
  121. _rm_utmp(line,flag)
  122. char *line;
  123. int flag;
  124.    {
  125.    int tty;
  126.    int fd;
  127.    FILE *file;
  128.    char *fgets(), *rindex();
  129.    char buff[32];
  130.    int neof = 0;
  131.  
  132.    /* find ttyslot */
  133.  
  134.    if (line == (char *) 0) 
  135.       return(-1);
  136.  
  137.    if (rindex(line,'/'))
  138.       line = rindex(line,'/')+1;
  139.  
  140.    if (file = fopen(TTYS,"r")) {
  141.       for(tty=1; neof = fgets(buff,sizeof(buff),file) != NULL;tty++)
  142.          if (strncmp(line,buff+2,strlen(line)) == 0) break;
  143.       fclose(file);
  144.       }
  145.    else {
  146.       return(-2);
  147.       }
  148.       
  149.    /* zap utmp entry */
  150.  
  151.    if ( neof &&(fd = open(UTMP,O_RDWR))>=0) {
  152.       lseek(fd,(long) (tty*sizeof(entry)),0);
  153.       if (flag) {
  154.      save_slot = tty;
  155.          read(fd,&save_entry,sizeof(entry));
  156.          lseek(fd,(long) (tty*sizeof(entry)),0);
  157.          }
  158.       write(fd,zap,sizeof(entry));
  159.       close(fd);
  160.       return(tty);
  161.       }
  162.    return(-1);
  163.    }
  164.